package org.myspringframework.beans.factory.config.beandefinition;

import lombok.Data;
import org.myspringframework.beans.factory.config.beandefinition.PropertyValue;

import java.util.ArrayList;
import java.util.List;

//封装类中指定的属性值
@Data
public class PropertyValues {

    //因为类中属性可能会有很多，还需要定义一个集合包装下
    private final List<PropertyValue> propertyValueList = new ArrayList<>();

    public void addPropertyValue(PropertyValue pv) {
        for (int i = 0; i < propertyValueList.size(); i++) {
            PropertyValue currentPv = this.propertyValueList.get(i);
            if (currentPv.getName().equals(pv.getName())) {
                propertyValueList.remove(i);
                break;
            }
        }
        this.propertyValueList.add(pv);
    }

    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue pv : this.propertyValueList) {
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }

}